import org.eclipse.swt.widgets.*;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;

import java.util.ArrayList;

public class IssueEditor extends ItemEditor<Issue>
{
    public IssueEditor(Judicator k, Shell parent, String title, Issue target)
    {
        super(k, parent, title, target, 750, 430);
    }

    private List teams, concernedTeams;
    private ArrayList<Team> guiltyTeams, innocentTeams;

    private Label teamsLabel, concernedLabel, descLabel, valLabel, taskLabel;
    private Text desc, value;
    private Button include, exclude;
    private Combo task;

    protected void _init()
    {
        guiltyTeams = new ArrayList<Team>();
        innocentTeams = new ArrayList<Team>();

        teams = new List(shell, SWT.BORDER | SWT.MULTI | SWT.V_SCROLL);
        concernedTeams = new List(shell, SWT.BORDER | SWT.MULTI | SWT.V_SCROLL);
        teamsLabel = new Label(shell, SWT.SHADOW_OUT);
        concernedLabel = new Label(shell, SWT.SHADOW_OUT);
        descLabel = new Label(shell, SWT.SHADOW_OUT);
        valLabel = new Label(shell, SWT.SHADOW_OUT);
        taskLabel = new Label(shell, SWT.SHADOW_OUT);
        desc = new Text(shell, SWT.BORDER | SWT.MULTI);
        value = new Text(shell, SWT.BORDER);
        include = new Button(shell, SWT.PUSH);
        exclude = new Button(shell, SWT.PUSH);
        task = new Combo(shell, SWT.DROP_DOWN | SWT.READ_ONLY);

        descLabel.setBounds(20, 25, 70, 25);
        descLabel.setText("description:");
        desc.setBounds(20, 50, 330, 210);
        desc.setEditable(true);

        valLabel.setBounds(20, 290, 30, 25);
        valLabel.setText("value:");
        value.setBounds(20, 315, 60, 25);
        value.setEditable(true);

        taskLabel.setBounds(110, 290, 30, 25);
        taskLabel.setText("task:");
        task.setBounds(110, 315, 240, 25);
        for (int i = 0; i < judicator.tasks.size(); i++)
        {
            task.add(judicator.tasks.get(i).name);
        }
        if (judicator.tasks.size() > 0) task.select(0);

        concernedLabel.setBounds(380, 25, 60, 30);
        concernedLabel.setText("guilty:");

        teamsLabel.setBounds(590, 25, 50, 30);
        teamsLabel.setText("innocent:");

        concernedTeams.setBounds(380, 50, 140, 340);
        teams.setBounds(590, 50, 140, 340);

        ok.setBounds(175,400,60,30);
        cancel.setBounds(285,400,60,30);

        exclude.setBounds(535, 190, 40, 40);
        exclude.setText("<");
        exclude.addSelectionListener(new SelectionAdapter()
        {   public void widgetSelected(SelectionEvent e) {includeTeams();}});

        include.setBounds(535, 260, 40, 40);
        include.setText(">");
        include.addSelectionListener(new SelectionAdapter()
        {   public void widgetSelected(SelectionEvent e) {excludeTeams();}});

        innocentTeams.addAll(judicator.teams);
        for (Team t : judicator.teams) teams.add(t.getLabel());

        desc.setFocus();
    }

    protected void _assumeData()
    {
        concernedTeams.removeAll();
        teams.removeAll();
        innocentTeams.clear();
        guiltyTeams.clear();

        desc.setText(target.comment);
        value.setText(""+target.value);
        int taskIndex = judicator.tasks.indexOf(target.task);
        if (taskIndex >= 0) task.select(taskIndex);

        for (Team t : judicator.teams)
        {
            if (target.teams.contains(t))
            {
                concernedTeams.add(t.getLabel());
                guiltyTeams.add(t);
            }
            else
            {
                teams.add(t.getLabel());
                innocentTeams.add(t);
            }
        }
    }

    private boolean copyData()
    {
        target.comment = desc.getText();

        int taskIndex = task.getSelectionIndex();
        if (taskIndex >= 0) target.task = judicator.tasks.get(taskIndex);
        else target.task = null;

        if (value.getText().length() == 0)
        {
            target.value = 0.0;
        }
        else
        {
            try
            {
                target.value = Double.parseDouble(value.getText());
            }
            catch (NumberFormatException e)
            {
                AlertSender.sendAlert(value.getText() + " is not a valid value");
                return false;
            }
        }

        target.teams.clear();
        for (Team t : guiltyTeams)
        {
            target.addTeam(t);
        }

        return true;
    }

    protected void _addItem()
    {
        target = new Issue();
        if (copyData())
        {
            judicator.addIssue(target);
            close();
        }
    }

    protected void _modifyItem()
    {
        if (copyData())
        {
            judicator.update(target);
            close();
        }
    }

    protected void _close()
    {
        judicator.mainWindow.issueEditor = null;
    }

    private void rebuildLists()
    {
        concernedTeams.removeAll();
        teams.removeAll();

        for (Team t : innocentTeams) teams.add(t.getLabel());
        for (Team t : guiltyTeams) concernedTeams.add(t.getLabel());
    }

    private void includeTeams()
    {
        int[] in = teams.getSelectionIndices();
        ArrayList<Team> temp = new ArrayList<Team>();

        for (int i = 0; i < in.length; i++)
        {
            temp.add(innocentTeams.get(in[i]));
        }

        innocentTeams.removeAll(temp);
        guiltyTeams.addAll(temp);

        rebuildLists();
    }

    private void excludeTeams()
    {
        int[] out = concernedTeams.getSelectionIndices();
        ArrayList<Team> temp = new ArrayList<Team>();

        for (int i = 0; i < out.length; i++)
        {
            temp.add(guiltyTeams.get(out[i]));
        }

        guiltyTeams.removeAll(temp);
        innocentTeams.addAll(temp);

        rebuildLists();
    }
}
